package com.java.controller;

import com.java.common.enums.YseOrNo;
import com.java.common.utils.R;
import com.java.pojo.Carousel;
import com.java.pojo.Category;
import com.java.pojo.vo.CategoryVO;
import com.java.pojo.vo.NewItemsVO;
import com.java.service.CarouselService;
import com.java.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jl
 * Created on 2020/10/15
 */
@Api(tags = "首页相关接口")
@RequestMapping("/index")
@RestController
@AllArgsConstructor
public class IndexController {

    private final CarouselService carouselService;
    private final CategoryService categoryService;

    @ApiOperation("获取首页轮播图列表")
    @GetMapping("/carousel")
    public R carousel() {
        List<Carousel> list = carouselService.queryAll(YseOrNo.YES.code);
        return R.ok(list);
    }

    /**
     * 首页分类展示需求:
     * 1.首次渲染首页只查询一级分类
     * 2.懒加载.鼠标移动到一级分类上,再加载其子分类
     */
    @ApiOperation("获取商品一级分类")
    @GetMapping("/cats")
    public R cats() {
        List<Category> list = categoryService.queryAllRootLevelCat();
        return R.ok(list);
    }

    @ApiOperation("获取商品子分类")
    @GetMapping("/subCat/{rootSubId}")  // 路径参数
    public R subCat(@PathVariable("rootSubId") Integer rootSubId) {
        List<CategoryVO> list = categoryService.getSubCatList(rootSubId);
        return R.ok(list);
    }

    @ApiOperation("查询每个一级分类下的最新6条商品数据")
    @GetMapping("/sixNewItems/{rootSubId}")  // 路径参数
    public R sixNewItems(@PathVariable("rootSubId") Integer rootSubId) {
        List<NewItemsVO> list = categoryService.getSixNewItemLazy(rootSubId);
        return R.ok(list);
    }


}
